function[w] = percoMultiClass(X, t, maxEpoches)
% Calculates the perceptron weight vectors w for the training set (X; t)
% using the training method described in the lectures. The number of weight
% vectors returned corresponds to the number of classes; every weight
% vector defines a perceptron that compares one class against all the
% others.
%
%   INPUT
%   X...........The training data; the training vectors are stored as
%               columns. The function takes care of converting them into
%               homogeneous coordinates.
%   t...........A vector containing the targets (class labels). Must have
%               the same size as the number of columns in X.
%   maxEpoches..A number, setting the maximum count of iterations; if
%               reached without finding a solution an Exception is thrown.
%   OUTPUT
%   w...........The weights in homogenous coordinates (so the first row
%               in this matrix are the negative theta). The last row tells
%               if the algorithm converged within maxEpoches, for every
%               class separatly.

    N = size(X, 2);
    if (N ~= size(t))
        err = MException('EFMEGrB4:sizeMismatch', 'The number of columns in X and the size of t must match');
        throw(err);
    end
    % init w, adapt X and t
    X = [ones(1, N); X];
    w = zeros(size(X, 1) + 1, max(t));
    for class = 1 : max(t)
        misclassified = false;
        weight = zeros(size(X, 1), 1);
        target = t;
        target(t ~= class) = -1;
        target(t == class) = 1;
        for epoche = 1 : maxEpoches
            for i = 1 : N
                temp = X(:, i) * target(i);
                if weight' * temp <= 0
                    misclassified = true;
                    weight = weight + temp;
                end
            end
            if ~misclassified
                break;
            end
        end
        w(:,class) = [weight; ~misclassified];
    end
end
